package com.example.crawler.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.example.crawler.mapper.CrawlerAllMapper;

import io.swagger.annotations.ApiOperation;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.io.IOException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/all/v1/")
public class CrawlerAllPropCompany {

    @Resource
    private CrawlerAllMapper allMapper;


    @GetMapping(value = "/queryAllProps")
    @ApiOperation("查询沪深/北交所/香港/美国/纽约拟上市数据")
    public void queryAllProps() throws IOException {
        // 1、爬取沪深数据
        // 2、爬取北交所数据
        List<Map<String, Object>> maps = allMapper.queryAllProps();
        System.out.println(maps);
        // 3、香港交易所数据

        // 4、纽约交易所数据

        // 5、

    }

    @GetMapping(value = "/getAllProps")
    @ApiOperation("爬取沪深/北交所/香港/美国/纽约拟上市数据")
    public void getAllProps() throws IOException {
        // 1、爬取沪深数据
        crawlerHsProps();

        // 2、爬取北交所数据
        crawlerBJProps();

        // 3、香港交易所数据

        // 4、纽约交易所数据

        // 5、

    }

    private void crawlerBJProps() {
        for (int i = 0; i <= 168; i++) {
            System.out.println(i);
            execute1(i);
        }
        System.out.println("执行结束！！！！！！！！！");
    }


    private void execute1(int i) {
        // 网址
        String url = "http://www.bse.cn/disclosureInfoController/zoneInfoResult.do?callback=jQuery331_1668567064387";
//        String url = "http://www.bse.cn/projectNewsController/infoResult.do?callback=jQuery331_1669962332639";
        HttpClient httpClient = new HttpClient();

        //创建HttpGet请求
        Date date = new Date();//获取当前的日期
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        Calendar c1 = Calendar.getInstance();
        c1.setTime(date);
        c1.add(Calendar.YEAR, -1);

        PostMethod postMethod = null;
        postMethod = new PostMethod(url);
        postMethod.setRequestHeader("User-Agent", "PostmanRuntime/7.29.2");
        NameValuePair[] data = {
                new NameValuePair("page", String.valueOf(i)),
                new NameValuePair("disclosureType", "9530"),
                new NameValuePair("startTime", df.format(c1.getTime())),
                new NameValuePair("endTime", df.format(date))
        };
        postMethod.setRequestBody(data);

        CloseableHttpResponse response = null;
        execute2(httpClient, postMethod, response);
    }

    private void execute2(HttpClient httpClient, PostMethod postMethod, CloseableHttpResponse response) {
        try {
            //使用HttpClient发起请求
            httpClient.executeMethod(postMethod); // 执行POST方法
            String result = postMethod.getResponseBodyAsString();
            //打印数据
            System.out.println(result.substring(24, result.length() - 1));
            if (!StringUtils.isEmpty(result.substring(24, result.length() - 1))) {
                List<Map> maps = JSONArray.parseArray(result.substring(24, result.length() - 1), Map.class);
                List<Map> mapList = JSONArray.parseArray(JSON.parseObject(maps.get(0).get("listInfo").toString(), Map.class).get("content").toString(), Map.class);

                for (Map map : mapList) {
                    map.put("stock_code", map.get("companyCd"));
                    map.put("cname", map.get("companyName"));
                    map.put("overseas_flag", "1");
                    map.put("jys", "北京交易所");
                    map.put("shijian", map.get("publishDate"));
                    map.put("gonggao", map.get("disclosureTitle"));
                    map.put("pdf_url", "https://www.bse.cn" + map.get("destFilePath"));
                    if (map.get("disclosureTitle").toString().contains("(") && map.get("disclosureTitle").toString().contains(")")) {
                        map.put("gong_gao_type", map.get("disclosureTitle").toString().substring(map.get("disclosureTitle").toString().indexOf("（")+1, map.get("disclosureTitle").toString().indexOf("）")));
                    }

                    allMapper.insert(map);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //释放连接
            if (response != null) {
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
//                httpClient.close();
            }
        }
    }


    private void crawlerHsProps() throws IOException {
        // 网址
        List<String> urlList = new ArrayList<>();
        // 上海
        for (int i = 1; i <= 50; i++){
            String url = "http://eid.csrc.gov.cn/ipo/1010/index_" + i + ".html";
            urlList.add(url);
        }
        // 深圳
        for (int i = 1; i <= 50; i++){
            String url = "http://eid.csrc.gov.cn/ipo/1017/index_" + i + ".html";
            urlList.add(url);
        }

        int index = 1;
        System.out.println(urlList.size());
        for (String url : urlList) {
            System.out.println(index++ + ":               " +  url);
            Document document = Jsoup.parse(new URL(url), 300000);
            Elements elements = document.getElementsByClass("m-table2 m-table2-0");
            Element el = elements.first();
            List<Map<String, String>> tableColumnData = getTableColumnData(el, "1");
            // System.out.println(JSONObject.toJSONString(tableColumnData));
            for (Map<String, String> tableColumnDatum : tableColumnData) {
                allMapper.insert(tableColumnDatum);
            }

        }
        System.out.println("爬取结束！");

    }


    /**
     * @param table
     * @param rowSelectRange
     * @return
     */
    public List<Map<String, String>> getTableColumnData(Element table, String rowSelectRange) {
        List<Map<String, String>> companyList = new ArrayList<>();

        Elements trs = table.select("tr");
        if (!StringUtils.isEmpty(rowSelectRange)) {
            String[] deleteRows = rowSelectRange.split(",");
            int offsetIndex = 0;
            for (int i = deleteRows.length - 1; i >= 0; i--) {
                int index = Integer.parseInt(deleteRows[i]);
                if (index < 0) {
                    index = Math.abs(index);
                    index = trs.size() - (index - offsetIndex);
                    trs.remove(index);
                    offsetIndex++;
                } else {
                    trs.remove(index - 1);
                }
            }
        }
        for (Element tr : trs) {
            Elements tds = tr.select("td");
            // th 和 td 混合的情况下，取子元素
            if (tr.select("th").size() > 0) {
                tds = tr.children();
            }
            int index = 0;
            Map<String, String> map = new HashMap<>();
            String urlPDF = String.valueOf(tr.attributes().get("onclick"));
            if(urlPDF.contains("(") && urlPDF.contains(")") && urlPDF.contains(".pdf") && urlPDF.contains("http")){
                map.put("pdf_url", urlPDF.substring(urlPDF.indexOf("(")+2, urlPDF.indexOf(".pdf")+4));
            } else {
                map.put("pdf_url", null);
            }
            map.put("overseas_flag", "1");

            for (Element td : tds) {
                switch (index++) {
                    case 0:
                        if (StringUtils.isEmpty(td.text().trim())) continue;
                        map.put("cname", td.text().trim());
                        break;
                    case 1:
                        if (StringUtils.isEmpty(td.text().trim())) continue;
                        map.put("gong_gao_type", td.text().trim());
                        break;
                    case 2:
                        if (StringUtils.isEmpty(td.text().trim())) continue;
                        map.put("bankuai", td.text().trim());
                        break;
                    case 3:
                        if (StringUtils.isEmpty(td.text().trim())) continue;
                        map.put("jigou", td.text().trim());
                        break;
                    case 4:
                        if (StringUtils.isEmpty(td.text().trim())) continue;
                        map.put("shijian", td.text().trim());
                        break;
                    case 5:
                        if (StringUtils.isEmpty(td.text().trim())) continue;
                        map.put("gonggao", td.text().trim());
                        break;
                }
            }
            map.put("jys", map.get("bankuai").contains("深") ? "深圳交易所" : "上海交易所");
            companyList.add(map);
        }
        return companyList;
    }


}
